﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Database.Model
{
    public partial class CAD_EMPRESA
    {

        public static CAD_EMPRESA Find(Int64 Id)
        {
            try
            {
                using (Entities context = new Entities())
                {
                    return context.CAD_EMPRESA.Find(Id);
                }
            }
            catch (DbEntityValidationException e)
            {
                String Error = String.Empty;
                foreach (var eve in e.EntityValidationErrors)
                {
                    Error += String.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    Console.WriteLine(Error);

                    foreach (var ve in eve.ValidationErrors)
                    {
                        Error += String.Format("- Propriedade: \"{0}\", Erro: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        Console.WriteLine("- Propriedade: \"{0}\", Erro: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw new Exception(Error);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Create()
        {
            using (var DBctx = new Entities())
            {
                DBctx.CAD_EMPRESA.Add(this);
                DBctx.SaveChanges();
            }
        }

        public void Save()
        {
            using (var DBContext = new Entities())
            {
                DBContext.CAD_EMPRESA.Attach(this);
                DBContext.Entry(this).State = EntityState.Modified;
                DBContext.SaveChanges();
            } 
        }

        public IEnumerable<CAD_CLIENTE> FindAll()
        {
            using (var DBctx = new Entities())
            {
                return DBctx.CAD_CLIENTE.Select(c => c);
            }
        }

        public void deleta()
        {
            using (var DBContext = new Entities())
            {
                DBContext.CAD_EMPRESA.Attach(this);
                DBContext.Entry(this).State = EntityState.Deleted;
                DBContext.SaveChanges();
            } 
        }

        public static object GridFiltroEmpresa()
        {
            Entities context = new Entities();
            var c = (
                    from v in context.CAD_EMPRESA
                    select new
                    {
                        v.EMPRESA_ID,
                        v.CNPJ,
                        v.RAZAO,
                        v.TELEFONE,
                        v.NOME_FANTASIA
                    }
                );

            return c.ToList();
        }
    }
}
